package com.edu.publisher.controller;

import com.edu.publisher.bean.*;
import com.edu.publisher.service.TradeStatsService;
import com.edu.publisher.util.DateFormatUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.List;

/**
 * @author zcx
 * @create 2022-10-19 20:13
 */
@RestController
public class TradeStatsController {

    @Autowired
    private TradeStatsService tradeStatsService;

    @RequestMapping("/cau")
    public String getCAU(@RequestParam(value = "date",defaultValue = "0") Integer date) {
        if (date == 0) {
            date = DateFormatUtil.now();
        }
        BigDecimal cau = tradeStatsService.getCAU(date);
        String json = "{\"status\": 0,\"data\": " + cau + "}";
        return json;
    }

    @RequestMapping("/total")
    public String getFinalAmount(@RequestParam(value = "date",defaultValue = "0") Integer date) {
        if (date == 0) {
            date = DateFormatUtil.now();
        }
        Double finalAmount = tradeStatsService.getFinalAmount(date);
        if (finalAmount == null) {
            return "";
        }
        String json = "{\"status\": 0,\"data\": " + finalAmount + "}";
        return json;
    }

    @RequestMapping("/stats")
    public String getTradeStats(@RequestParam(value = "date",defaultValue = "0") Integer date) {
        if (date == 0) {
            date = DateFormatUtil.now();
        }
        List<TradeStatsBean> tradeStats = tradeStatsService.getTradeStats(date);
        if (tradeStats == null) {
            return "";
        }
        StringBuilder rows = new StringBuilder("[");
        for (int i = 0; i < tradeStats.size(); i++) {
            TradeStatsBean tradeStatsBean = tradeStats.get(i);
            String type = tradeStatsBean.getType();
            Integer finalAmount = tradeStatsBean.getFinalAmount();
            rows.append("{\n" +
                    "        \"type\": \"" + type + "\",\n" +
                    "        \"value\": \"" + finalAmount + "\"\n" +
                    "      }\n");
            if (i < tradeStats.size() - 1) {
                rows.append(",");
            } else {
                rows.append("]");
            }
        }
        String json = "{\n" +
                "  \"status\": 0,\n" +
                "  \"data\": {\n" +
                "    \"columns\": [\n" +
                "      {\n" +
                "        \"name\": \"指标类型\",\n" +
                "        \"id\": \"type\"\n" +
                "      },\n" +
                "      {\n" +
                "        \"name\": \"度量值\",\n" +
                "        \"id\": \"value\"\n" +
                "      }\n" +
                "    ],\n" +
                "    \"rows\": \n" +
                "        "+ rows + "\n" +
                "  }\n" +
                "}";
        return json;
    }

    @RequestMapping("/province")
    public String getProvinceAmount(@RequestParam(value = "date", defaultValue = "0") Integer date) {
        if (date == 0) {
            date = DateFormatUtil.now();
        }
        List<TradeProvinceOrderAmount> provinceAmountList = tradeStatsService.getProvinceAmount(date);
        StringBuilder jsonB = new StringBuilder("{\"status\": 0,\"data\": {\"mapData\": [");
        for (int i = 0; i < provinceAmountList.size(); i++) {
            TradeProvinceOrderAmount orderAmount = provinceAmountList.get(i);
            jsonB.append("{\"name\": \"" + orderAmount.getProvinceName() + "\",\"value\": " + orderAmount.getOrderAmount() + "}");
            if(i < provinceAmountList.size() - 1){
                jsonB.append(",");
            }
        }
        jsonB.append("],\"valueName\": \"交易额\"}}");
        return jsonB.toString();

    }

    //获取某一天各来源的交易额
    @RequestMapping("/source")
    public String getScFinalAmount(@RequestParam(value = "date",defaultValue = "0") Integer date){
        if (date == 0) {
            date = DateFormatUtil.now();
        }
        List<TradeScFinalAmount> tradeScFinalAmountList = tradeStatsService.getScFinalAmount(date);
        if (tradeScFinalAmountList == null) {
            return "";
        }
        StringBuilder categories = new StringBuilder("[");
        StringBuilder scFinalAmountValues = new StringBuilder("[");

        for (int i = 0; i < tradeScFinalAmountList.size(); i++) {
            TradeScFinalAmount tradeScFinalAmount = tradeScFinalAmountList.get(i);
            String sc = tradeScFinalAmount.getSourceName();
            Double scFinalAmount = tradeScFinalAmount.getFinalAmount();

            categories.append("\"").append(sc).append("\"");
            scFinalAmountValues.append("\"").append(scFinalAmount).append("\"");

            if (i < tradeScFinalAmountList.size() - 1) {
                categories.append(",");
                scFinalAmountValues.append(",");
            } else {
                categories.append("]");
                scFinalAmountValues.append("]");
            }
        }

        return  "{\n" +
                "  \"status\": 0,\n" +
                "  \"data\": {\n" +
                "    \"categories\": \n" +
                "       " + categories +
                "    ,\n" +
                "    \"series\": [\n" +
                "      {\n" +
                "        \"name\": \"各来源销售额\",\n" +
                "        \"data\": \n" +
                "           "+ scFinalAmountValues + "\n" +
                "      }\n" +
                "    ]\n" +
                "  }\n" +
                "}";
    }

    //获取某一天各来源的下单用户数
    @RequestMapping("/source_user")
    public String getScOdUvCt(@RequestParam(value = "date",defaultValue = "0") Integer date){
        if (date == 0) {
            date = DateFormatUtil.now();
        }
        List<TradeScOdUvCt> tradeScOdUvCtList = tradeStatsService.getScOdUvCt(date);
        if (tradeScOdUvCtList == null) {
            return "";
        }
        StringBuilder categories = new StringBuilder("[");
        StringBuilder scOdUvCtValues = new StringBuilder("[");

        for (int i = 0; i < tradeScOdUvCtList.size(); i++) {
            TradeScOdUvCt tradeScOdUvCt = tradeScOdUvCtList.get(i);
            String sc = tradeScOdUvCt.getSourceName();
            Integer scOdUvCt = tradeScOdUvCt.getUvCt();

            categories.append("\"").append(sc).append("\"");
            scOdUvCtValues.append("\"").append(scOdUvCt).append("\"");

            if (i < tradeScOdUvCtList.size() - 1) {
                categories.append(",");
                scOdUvCtValues.append(",");
            } else {
                categories.append("]");
                scOdUvCtValues.append("]");
            }
        }

        return  "{\n" +
                "  \"status\": 0,\n" +
                "  \"data\": {\n" +
                "    \"categories\": \n" +
                "       " + categories +
                "    ,\n" +
                "    \"series\": [\n" +
                "      {\n" +
                "        \"name\": \"各来源下单用户数\",\n" +
                "        \"data\": \n" +
                "           "+ scOdUvCtValues + "\n" +
                "      }\n" +
                "    ]\n" +
                "  }\n" +
                "}";
    }
    //获取某一天各来源的下单次数
    @RequestMapping("/source_order")
    public String getScOdCt(@RequestParam(value = "date",defaultValue = "0") Integer date){
        if (date == 0) {
            date = DateFormatUtil.now();
        }
        List<TradeScOdCt> tradeScOdCtList = tradeStatsService.getScOdCt(date);
        if (tradeScOdCtList == null) {
            return "";
        }
        StringBuilder categories = new StringBuilder("[");
        StringBuilder scOdCtValues = new StringBuilder("[");

        for (int i = 0; i < tradeScOdCtList.size(); i++) {
            TradeScOdCt tradeScOdCt = tradeScOdCtList.get(i);
            String sc = tradeScOdCt.getSourceName();
            Integer scOdCt = tradeScOdCt.getOdCt();

            categories.append("\"").append(sc).append("\"");
            scOdCtValues.append("\"").append(scOdCt).append("\"");

            if (i < tradeScOdCtList.size() - 1) {
                categories.append(",");
                scOdCtValues.append(",");
            } else {
                categories.append("]");
                scOdCtValues.append("]");
            }
        }

        return  "{\n" +
                "  \"status\": 0,\n" +
                "  \"data\": {\n" +
                "    \"categories\": \n" +
                "       " + categories +
                "    ,\n" +
                "    \"series\": [\n" +
                "      {\n" +
                "        \"name\": \"各来源下单次数\",\n" +
                "        \"data\": \n" +
                "           "+ scOdCtValues + "\n" +
                "      }\n" +
                "    ]\n" +
                "  }\n" +
                "}";

    }
    //获取某一天各来源的下单转化率
    @RequestMapping("/source_pc")
    public String getScOdPeCo(@RequestParam(value = "date",defaultValue = "0") Integer date){
        if (date == 0) {
            date = DateFormatUtil.now();
        }
        List<TradeScOdPercentConversion> tradeScOdPeCoList = tradeStatsService.getScOdPeCo(date);
        if (tradeScOdPeCoList == null) {
            return "";
        }
        StringBuilder categories = new StringBuilder("[");
        StringBuilder scOdPeCoValues = new StringBuilder("[");

        for (int i = 0; i < tradeScOdPeCoList.size(); i++) {
            TradeScOdPercentConversion tradeScOdCt = tradeScOdPeCoList.get(i);
            String sc = tradeScOdCt.getSourceName();
            Double scOdPeCo = tradeScOdCt.getOdPc();

            categories.append("\"").append(sc).append("\"");
            scOdPeCoValues.append("\"").append(scOdPeCo).append("\"");

            if (i < tradeScOdPeCoList.size() - 1) {
                categories.append(",");
                scOdPeCoValues.append(",");
            } else {
                categories.append("]");
                scOdPeCoValues.append("]");
            }
        }

        return  "{\n" +
                "  \"status\": 0,\n" +
                "  \"data\": {\n" +
                "    \"categories\": \n" +
                "       " + categories +
                "    ,\n" +
                "    \"series\": [\n" +
                "      {\n" +
                "        \"name\": \"各来源下单转化率\",\n" +
                "        \"data\": \n" +
                "           "+ scOdPeCoValues + "\n" +
                "      }\n" +
                "    ]\n" +
                "  }\n" +
                "}";
    }
}
